<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class OldOperationController extends Controller
{
    /*
     * @@运营统计的首页
     */
    public function index(){
        //统计会员总人数
        $persons=$this->peoples();
       // 统计男女所占的比例
       $persex= $this->datasex();
       //统计各年龄段所在的比例
        $ages=$this->getagebyid();
        $count15 = 0;
        $count20 = 0;
        $count30 = 0;
        $count50 = 0;
        $count51 = 0;
        foreach($ages as $v){
            if($v<15){
                $count15++;
            }elseif ($v>=15 && $v<20){
                $count20++;
            }elseif ($v>=20 && $v<30){
                $count30++;
            }elseif ($v>=30 && $v<50){
                $count50++;
            }else{
                $count51++;
            }
        }
//        dd($count15,$count20,$count30,$count50,$count51);
        return view('admin/operation/operationlist')
            ->with('persons',$persons)
            ->with('persex',$persex)
            ->with('count15',$count15)
            ->with('count20',$count20)
            ->with('count30',$count30)
            ->with('count50',$count50)
            ->with('count51',$count51);
    }
    /*
     * @@南丁格尔圆环形图的数据交互
     * @功能:统计人数
     */
    public function peoples(){
        $data= User::all()->count();
        return $data;
    }
    /*
     * @@南丁格尔圆环形图的数据交互
     * @功能:会员的性别统计
     */
    public function datasex(){
        $data=[];
        $data['男']=User::where('sex','=','1')->count();
        $data['女']=User::where('sex','=','0')->count();
        return $data;
    }
    /*
     * @@南丁格尔圆环形图的数据交互
     * @功能:根据身份证号来判断年龄段的会员
     */
    public function getagebyid(){
         // 查询数据库中的id_card号
        $dataages=User::select('id_card')->get();
        $data=$dataages->toArray();
        foreach($data as $v) {
            $date = strtotime(substr($v['id_card'] , 6, 8));
            $today = strtotime('today');//获得今日的时间戳
            $diff = floor(($today - $date) / 86400 / 365);
            $age = strtotime(substr( $date, 6, 8) . ' + ' . $diff . 'years') > $today ? ($diff + 1) : $diff;
            //挨个获取
            $dates[] =$age ;
        }
        return $dates;
    }
    /*
     * @@折线统计图的数据交互
     * @功能:折线统计图的==>营收报表
     */
    public function firminfo(){

    }
}
